package org.example.com.editor.command.workspaceCommand;


import org.example.com.editor.command.Command;
import org.example.com.editor.logger.LogManager;
import org.example.com.editor.ui.ConsoleUI;
import org.example.com.editor.workspace.Workspace;

import java.util.List;

/**
 * 加载文件命令：将文件加载到工作区并设为活动文件
 */
public class LoadCommand implements Command {
    private final LogManager logManager;

    public LoadCommand(LogManager logManager) {
        this.logManager = logManager;
    }

    @Override
    public void execute(Workspace workspace, ConsoleUI ui) throws Exception {
        // 获取命令参数（格式：load <filePath>）
        String[] args = ui.getCommandArgs();
        if (args.length != 2) {
            ui.printError("用法：load <文件路径>");
            return;
        }
        String filePath = args[1];
        ui.printInfo("加载文件：" + filePath);
        workspace.loadFile(filePath);

        List<String> content = workspace.getActiveEditor().getContent();
        if (!content.isEmpty()) {
            String firstLine = content.get(0);
            logManager.checkAndEnableLogForFile(filePath, firstLine);
        }

        ui.printInfo("已加载，当前活动文件：" + filePath);

        logManager.onCommandExecuted(filePath, args);
    }

    @Override
    public String getName() {
        return "load";
    }
}